package ShopStyle.htc;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.Log;

public class MagItem {
	private int _x,_y, _alpha;	//Displayed position
	private int _fixedWidth;
	private int _calcHeight;
	
	private String _resName;
	private Drawable _pic;
	private String[] _textLine = new String[3];
	private static final String PACKAGE_NAME = "ShopStyle.htc" ;
	private Context _context;
	private int _height;
	private int _width;
	private Paint _textPaint = new Paint();
	private int _textHeight;
	
	private String[] _allenString = new String[4];
	public void setAllenString(String[] allenString) {
		_allenString = allenString;
	}
	public MagItem(Context context, String pictureName, String textLine1, String textLine2, String textLine3) {
		
		_context = context;
		_resName = pictureName;
		Log.e ("MagItem", "Allocating resource : " + _resName);
		_textLine[0] = textLine1;
		_textLine[1] = textLine2;
		_textLine[2] = textLine3;
		_textPaint.setColor( Color.DKGRAY  );
		_textPaint.setTextSize(25);
		_textHeight = (int) ( _textPaint.getTextSize() );

		
		//_pic = this.getResources().getDrawable(R.drawable.pig);
		
		int resID = _context.getResources().getIdentifier(_resName , "drawable", _context.getPackageName() );
		_pic = _context.getResources().getDrawable( resID );
		if (_pic!=null) {
			Log.e("MagItem" , "Allocated resource [" + _resName + "]" + _pic.toString());
			_height = _pic.getIntrinsicHeight();
			_width = _pic.getIntrinsicWidth();
			Log.e("MagItem" , "  H[" + _height + "] W[" + _width + "]" );
			
		}
			
		
		//getResources().getIdentifier("bug", "drawable", "org.anddev.android.testproject")
		//pic1 = this.getResources().getDrawable(R.drawable.pig);
	}
	public void setParameters(int x, int y, int alpha, int fixedWidth) {
		this._x = x;
		this._y = y;
		if (alpha < 0) 	this._alpha = 128;	//Default
		else			this._alpha = alpha;
		if (fixedWidth < 0 )	this._fixedWidth = _width;
		else					this._fixedWidth = fixedWidth;
		
		_calcHeight = (int)( (double)(_height)*((double)(_fixedWidth)/_width) ); 
		
	}
	
	public int getX() {
		return _x;
	}
	public int getY() {
		return _y;
	}
	public boolean checkEvent(int localX,int localY , String tmpStr) {
		//Check if it is inside the Drawable ?
		if (0<= localX && localX <= _fixedWidth &&
			0<= localY && localY <= _calcHeight ) {
			
			Log.e ( "@checkEvent" , _allenString[0] + " is clicked");
			//[TODO] Please add necessary code here
		}
		else {
			for (int i=0;i<3;++i) {
				int tmpX = (int)(_textPaint.measureText(this._textLine[i] ) );
				int tmpY = localY - (_calcHeight + i*this._textHeight );
				if (	0<= localX && localX <= tmpX &&
						0<= tmpY && tmpY <= this._textHeight ) {
					Log.e ( "@checkEvent" , _allenString[i+1] + " is clicked");
				}
			}
		}
		return true;
	}
	public void drawOnCanvas(Canvas c) {
		if (c==null) return ;
		
		//Maintain the ratio, recalculate corresponding height
		_pic.setBounds(_x, _y, _x+_fixedWidth , _y+ _calcHeight );
		_pic.setAlpha(_alpha);
		_pic.draw(c);
		
		//Draw semi-trasparent rectangle
		/*
		c.drawRect
		paint.setAntiAlias(false);
		paint.setColor(Color.GREEN);
		canvas.drawRect(100, 5, 200, 30, paint);
		*/
		
		//Draw Lines
		_textPaint.setAlpha(_alpha);
		for (int i=0;i<3;++i) {
			c.drawText( _textLine[i] , _x , 
						_y+ _calcHeight + _textHeight + i*(_textHeight) , _textPaint); 

		}
		/*
		int textY = (int)((15 + 20 * ptr) * scale);
		c.drawText("id" + ptr + "=" + id, width - 55 * scale, textY, textPaint);
		*/
		
	}

}
